import Vue from "vue";
import App from "./App.vue";
import router from "./router";
import axios from "axios";
import ElementUI from "element-ui";
import "element-ui/lib/theme-chalk/index.css";

Vue.use(ElementUI);
Vue.prototype.$axios = axios;
Vue.filter("filterURL", url => {
  if (/^http/.test(url)) {
    return url;
  } else {
    return axios.defaults.baseURL + url;
  }
});
// 统一带上token
axios.interceptors.request.use(config => {
  if (!config.headers.Authorization && localStorage.getItem("token")) {
    config.headers.Authorization = "Bearer " + localStorage.getItem("token");
  }
  return config;
});
// 添加基准地址
// axios.defaults.baseURL = "http://liangwei.tech:3000";
axios.defaults.baseURL = "http://127.0.0.1:3000";
// 添加路由守卫
router.beforeEach((to, from, next) => {
  if (to.path != "/login" && !localStorage.getItem("token")) {
    router.push("/login");
  } else {
    next();
  }
});
Vue.config.productionTip = false;
new Vue({
  router,
  render: function(h) {
    return h(App);
  }
}).$mount("#app");
